#include <iostream>
#include <ratio>
#include <list>
#include <map>
#include "SomeTool.h"
#include "BTree.h"
#include "test.h"

using namespace std;

void test1()
{
	BTree<char, 3> bTree;
	auto bTreeInsert = [&](char i)
	{
		cout << "bTree.insert(" << i << ");" << endl;
		bTree.insert(i);
		bTree.show(cout, 1) << endl;
		cout << "bTree.verify()=" << bTree.verify() << endl;
	};
	auto bTreeInsertList = [&](std::initializer_list<char> il, bool detail = false)
	{
		bool first = true;
		if (!detail)
			cout << "bTree.insert(";
		for (auto i : il)
		{
			if (detail)
				cout << "bTree.insert(";
			else
			{
				if (first) first = false;
				else cout << ",";
			}

			cout << i;
			bTree.insert(i);

			if (detail)
			{
				cout << ");" << endl;
				bTree.show(cout, 1) << endl;
				cout << "bTree.verify()=" << bTree.verify() << endl;
			}
		}

		if (!detail)
		{
			cout << ");" << endl;
			bTree.show(cout, 1) << endl;

		}
	};

	bTreeInsertList({ 'A', 'C', 'J', 'K', 'G', 'N', 'O', 'M', 'R', 'S', 'P', 'Y', 'Z', 'X', 'T', 'U' , 'V' , 'D' , 'E', 'B', 'Q', 'L', 'F' }, true);

	char i = 0;
	while (cin >> i)
		bTreeInsert(i);
}

pair<list<char>::const_iterator, bool> find(const list<char>& lst, char c)
{
	auto it = lst.begin();
	while (it != lst.end() && (*it) < c)
		++it;

	return make_pair(it, *it == c);
}

void test2()
{
	list<char> ch = { 'B', 'C', 'E', 'S' };
	{
		auto [it, ret] = find(ch, 'C');
		cout << *it << "," << ret << endl;
	}
	{
		auto [it, ret] = find(ch, 'D');
		cout << *it << "," << ret << endl;
	}
	{
		auto [it, ret] = find(ch, 'P');
		cout << *it << "," << ret << endl;
	}
	{
		auto [it, ret] = find(ch, 'E');
		cout << *it << "," << ret << endl;
	}
}

void test3()
{
	list<char> ch = { 'B', 'C', 'E', 'S' };
	auto it1 = ch.begin();
	auto it2 = prev(it1);
}

void test4()
{
	BTree<char, 3> bTree;
	auto bTreeInsert = [&](char i)
	{
		cout << "bTree.insert(" << i << ");" << endl;
		bTree.insert(i);
		bTree.show(cout, 1) << endl;
		cout << "bTree.verify()=" << bTree.verify() << endl;
	};
	auto bTreeErase = [&](char i)
	{
		cout << "bTree.erase(" << i << ");" << endl;
		bTree.erase(i);
		bTree.show(cout, 1) << endl;
		cout << "bTree.verify()=" << bTree.verify() << endl;
	};
	auto bTreeInsertList = [&](std::initializer_list<char> il, bool detail = false)
	{
		bool first = true;
		if (!detail)
			cout << "bTree.insert(";
		for (auto i : il)
		{
			if (detail)
				cout << "bTree.insert(";
			else
			{
				if (first) first = false;
				else cout << ",";
			}

			cout << i;
			bTree.insert(i);

			if (detail)
			{
				cout << ");" << endl;
				bTree.show(cout, 1) << endl;

			}
		}

		if (!detail)
		{
			cout << ");" << endl;
			bTree.show(cout, 1) << endl;

		}
	};

	bTreeInsertList({ 'A', 'C', 'J', 'K', 'G', 'N', 'O', 'M', 'R', 'S', 'P', 'Y', 'Z', 'X', 'T', 'U' , 'V' , 'D' , 'E', 'B', 'Q', 'L', 'F' });
	cout << "bTree.verify()=" << bTree.verify() << endl;
	bTreeErase('F');
	bTreeErase('M');
	bTreeErase('G');
	bTreeErase('D');
	bTreeErase('B');
	bTreeInsert('W');
	char i = 0;
	while (cin >> i)
		bTreeErase(i);
}